# -*- coding: utf-8 -*- 
# @Time : 2021-07-19 23:44 
# @Author : derek.zhang 
# @File : serializers.py 
# @Software: PyCharm


from rest_framework import serializers

from idcs.models import Idc


class IdcSerializer(serializers.Serializer):
    '''
    IDC  序列化类
    '''
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(required=True, max_length=32, label='机房名称' , help_text='机房名称')
    address = serializers.CharField(required=True, max_length=256, label='机房地址' , help_text='机房地址')
    phone = serializers.CharField(required=True, max_length=15, label='手机号' , help_text='手机号')
    email = serializers.EmailField(required=True, label='email', help_text='email')
    letter = serializers.CharField(required=True, max_length=5, label='简称' , help_text='简称')


    def create(self, validated_data):
        return Idc.objects.create(**validated_data)

    def update(self, instance, validated_data):
        ''' help_text 接口文档  label 是接口测试页面 error_messages 错误提示'''
        instance.name = validated_data.get('name', instance.name)
        instance.address = validated_data.get('address', instance.address)
        instance.phone = validated_data.get('phone', instance.phone)
        instance.email = validated_data.get('email', instance.email)
        instance.letter = validated_data.get('letter', instance.letter)
        instance.save()
        return instance

